package com.yzy.fly.mapper;

import com.yzy.fly.entity.role.FlyTable;
import com.yzy.fly.entity.role.FlyTableColumn;
import com.yzy.fly.kernel.mapper.FlyBasicMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Mapper
public interface FlyTableMapper extends FlyBasicMapper<FlyTable> {

    @Select("select * from fly_table a where 1=1 ")
    List<FlyTable> selectAllList();

    @Select("select * from fly_table a where 1=1 and a.modelId = #{modelId}")
    FlyTable selectByModelId(@Param("modelId") Integer modelId);

    @Select("select * from fly_table a where 1=1 and a.systemId = #{systemId}")
    List<FlyTable> selectAllBySystemId(@Param("systemId") Integer systemId);

    @Select("select * from fly_table a where 1=1 and (a.id = #{parentId} or a.parentId = #{parentId}) and a.systemId = #{systemId}")
    List<FlyTable> selectAllBy(@Param("parentId") Integer parentId, @Param("systemId") Integer systemId);

    @Select("select * from fly_table a where 1=1 and a.defModelId = #{defModelId} and a.systemId = #{systemId}")
    List<FlyTable> selectAllByDefIdIdAndSystemId(@Param("defModelId") Integer defModelId, @Param("systemId") Integer systemId);

    /**
     * 批量插入（mysql）
     * @param entityList
     * @return
     */
    Integer insertBatchSomeColumn(Collection<FlyTable> entityList);

    @Select("select count(1) as count from information_schema.tables where table_name =#{tableName}")
    int checkExsitByTableName(@Param("tableName") String tableName);

    @Select({"SELECT a.column_name AS name,",
            " data_type AS type,",
            " case when data_type = 'varchar' then character_maximum_length else numeric_precision end as length ,",
            " numeric_scale AS numericScale,",
            " case when a.is_nullable = 'YES' then 1 else 0 end as notNull,",
            " case when a.column_name = t.column_name then 1 else 0 end as primaryKey,",
            " case when extra = 'auto_increment' then 1 else 0 end as autoIncrement,",
            " column_default AS defaultValue,",
            " column_comment AS comment ",
            " FROM information_schema.columns a left join information_schema.key_column_usage t",
            " on a.table_name= t.table_name and a.column_name = t.column_name ",
            " WHERE a.table_name=#{tableName}"})
    List<FlyTableColumn> descByTableName(@Param("tableName") String tableName);

    @Select("show create table ${tableName}")
    Map<String,String> DDL_CreateTable(@Param("tableName") String tableName);

    @Select("show tables")
    List<String> showTables();
    @Select("select * from ${tableName}")
    List<Map> data(@Param("tableName") String tableName);
}
